// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    temIds:['Wzq0Q8OpleP_KgHNxStTKJ6-8NdvRvqgJnioZ01w7bg'],
    showOkText:'',
    showOK:false,
    perfect_info: '',
    isRegister: '',   //判断是否是注册页面过来的
    noalter: false,
    shop: '默认店铺',
    arraySex: ['保密', '男', '女'],
    date: "2022-02-16",
    indexSex: 0,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    birthday: '',
    info: null,
    formatter (type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
  },
  onInput (event) {
    this.setData({
      currentDate: event.detail,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  formatDate (date = new Date()) {
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? '0' + m : m
    var d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    return y + '-' + m + '-' + d
  },
  onLoad: function (options) {
    // console.log('option', this.formatDate())
    wx.showLoading()
    if (options && options.isRegister) {
      this.getScoreSet()
      this.setData({
        isRegister: '1',
      })
    }
    var uinfo = wx.getStorageSync('uinfo')
    this.setData({
      id: uinfo.user.id,
      date: this.formatDate()
    })
    this.getInfo(uinfo.user.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '../user/user',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  filterTime () {

  },
  bindPickerChange: function (e) {
    console.log('性别', e.detail.value)
    this.setData({
      'info.sex': e.detail.value,
      indexSex: e.detail.value
    })
  },
  inputShop: function (e) {
    var that = this
    that.setData({
      'info.myshop': e.detail
    })

  },
  bindPickerChangeBirth: function (e) {
    var that = this
    that.setData({
      birthday: e.detail.value
    })
  },
  changeName: function (e) {
    console.log(e.detail)
    this.setData({
      'info.user_nickname': e.detail
    })
  },
  getScoreSet () {
    var that = this
    wx.$api.getScoreSet().then(res=>{
        that.setData({
          perfect_info: res.data.perfect_info
        })
    })
  },
  updateUserInfo: function (e) {
    var that = this
    let str = that.data.info.user_nickname.trim()
    console.log(str.length, "str.length")
    if (str.length >= 11) {
      wx.showToast({
        title: '昵称不能超过10字符',
        icon: 'none',
      })
      return
    }
    if (that.data.info.user_nickname == '') {
      wx.showModal({
        title: '提示',
        content: '请将信息填写完整',
        showCancel: false,
        success (res) {
        }
      })
    } else {
      wx.$api.updateUserInfo({
          id: that.data.id,
          user_nickname: that.data.info.user_nickname,
          sex: that.data.indexSex,
          myshop: that.data.info.myshop,
          birthday: that.data.birthday,
        }).then(res=>{
          if (res.isRegister == 1) {
            that.setData({
              showOK:true,
              showOkText:'保存成功!'
            })
          } else {
            that.setData({
              showOK:true,
              showOkText:'修改成功!'
            })
        }
      })
    }

  },
  
  setInfo(){// 订阅消息
    let that = this
    this.setData({
      showOK:false
    })
    wx.switchTab({
      url: '../user/user',
    })
    return
    if(that.data.info.is_perfect_info<1&&that.data.indexSex>0&&that.data.birthday){
      let shareData={
        user_id:that.data.info.id,
        type:3,
        other_type:'',
      }
      wx.requestSubscribeMessage({
          tmplIds: that.data.temIds,
          success (res) {
              if(res[that.data.temIds[0]]=='accept'){//同意
                shareData.other_type='1'
              }
              that.sendSetYue(shareData)
          },
          fail(error){
            wx.switchTab({
              url: '../user/user',
            })
            console.log(error,'123失败');
          }
        })
    }else{
      wx.switchTab({
        url: '../user/user',
      })
    }
  },
  sendSetYue(data){
    wx.$api.sendTemplate(data).then(res=>{
            if(res.code>0){
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
            wx.switchTab({
              url: '../user/user',
            })
    })
  },
  noAlter: function () {
    wx.showModal({
      title: '提示',
      content: '生日无法再次更改',
      showCancel: false,
      success (res) {

      }
    })
  },
  getInfo: function (id) {
    var that = this
    wx.$api.getUserInfo({
        userid: id
      }).then(res=>{
        that.setData({
          info: res.data,
          indexSex: res.data.sex,
          birthday: res.data.birthday
        })
        if (!res.data.myshop || res.data.myshop == null || res.data.myshop == 'null') {
          that.setData({
            'info.myshop': '',
          })
        }
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 100)
  }
})